package com.example.api.mq.component;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @name: RabbitMqListenter
 * @description: rabbitmq队列监听器
 * @author: xw.s
 * @date: 2021/5/31-14:14
 */
@Component
@Slf4j
public class RabbitMqListenter {

    /**
     * 启动监听rabbitmq队列
     * 可以监听多个队列
     * 多个监听者，每个消息只能被一个接收
     * 处理完一个message后才可以接收下一个消息
     */
//    @RabbitListener(queues = {"mall-queue"})
    public void receiveMessage(Message message){
        log.info("消息接收执行开始");
        log.info("接收到内容{}", new String(message.getBody()));
        log.info("消息接收执行结束");
    }

    /**
     * Object 是指发送的对象类型，可以直接获取
     * Channel 数据传输通道
     * @param message
     * @param o
     */
    @RabbitListener(queues = {"mall-queue"})
    public void receiveMessage(Message message, Object o, Channel channel) throws IOException {
        log.info("消息消费者正在接收消息");
        System.out.println(message);
        System.out.println(o);
        System.out.println(channel);
        long diliveryTag = message.getMessageProperties().getDeliveryTag();
        channel.basicAck(diliveryTag, false); //手动确认完成了哪个消息
        log.info("消息消费者正在接收消息结束");
    }
}
